<template>
<div>
  <div class="row-header">
    <el-button type="primary" size="small"  icon="el-icon-back" @click="toBack">返回</el-button>
  </div>
  <div class="split-line"></div>
  <div class="row-main">
    <el-card style="width: 700px; height: auto; margin: 5vh auto;">
      <el-form :model="form" ref="form" :rules="formRules" label-width="150px">
        <el-form-item label="流量池名称" prop="flowPoolName">
          <el-input size="small" style="width: 100%;" v-model="form.flowPoolName"></el-input>
        </el-form-item>
        <el-form-item label="所属运营商" prop="dictOperatorName">
          <el-select v-model="form.dictOperatorName" placeholder="请选择" style="width: 300px;" size="small">
            <el-option v-for="dictOperator in form.dictOperatorNameSelect" :key="dictOperator" :label="dictOperator" :value="dictOperator"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="a" label="共享套餐">
          <el-input placeholder="请输入内容" v-model="form.singleCardMeal" class="input-with-select" style="width: 80%">
            <el-select size="small" v-model="form.unit" placeholder="单位" style="width: 80px;" slot="append">
              <el-option label="GB" value="B"></el-option>
              <el-option label="MB" value="M"></el-option>
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="所属通道" prop="channelCompanyId">
          <el-select v-model="form.channelCompanyId" placeholder="请选择" style="width: 300px;" size="small">
            <el-option v-for="channelCompany in form.channelCompanyIdSelect" :key="channelCompany.id" :label="channelCompany.name" :value="channelCompany.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传Excel" prop="pem">
          <div class="file_upload_box">
            <input class="el" type="file" ref="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" @change="fileChangeHandle(fileList.file)" style="cursor: default;">
            <el-button class="excel" href="javascript:;" ref="fileName" v-model="form.fileName" :class="{active: this.form.fileName != '点击上传' &amp;&amp; this.form.fileName != ''}">点击上传</el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="submitPrimaryFlowPoolHandle('form')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</div>
</template>

<script>
  import { submitPrimaryFlowPool, getChannelcompanyForSelect, editPrimaryFlowPoolList } from '../../../api/getData'

  export default {
    name: 'EditPool',
    data() {
      return {
        unit: '',
        unitSelect: [
          {
            value: 'G',
            label: 'GB'
          },
          {
            value: 'M',
            label: 'MB'
          }
        ],
        fileRules: {
          file: [
            { required: true }
          ]
        },
        fileList: {
          file: ''
        },
        dictOperator: '',
        form: {
          flowPoolName: '',
          dictOperatorName: [],
          dictOperatorNameSelect: [],
          channelCompanyId: [],
          channelCompanyIdSelect: [],
          singleCardMeal: '',
          cardNumber: '',
          theoryFlow: '',
          containsFlow: '',
          virtualUsedFlow: '',
          fileName: '',
          file: ''
        },
        formRules: {
          flowPoolName: [
            { required: true, message: '请输入流量池名称', trigger: 'blur', min: 1, max: 60 }
          ],
          dictOperatorName: [{ required: true, message: '请选择所属运营商', trigger: 'blur' }],
          channelCompanyId: [{ required: true, message: '请选择所属通道', trigger: 'blur' }],
          singleCardMeal: [
            { required: true, message: '请输入共享套餐', trigger: 'blur' },
            { pattern: /^(([1-9][0-9]*)|(([0]\.\d{0,1}|[1-9][0-9]*\.\d{0,1})))$/, message: '请输入数字,精确到小数点后一位', trigger: 'blur' }
          ],
          unit: [{ required: true, message: '请选择套餐单位', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.firstPoolFlowId = this.$route.params.id
      this.initData()
      this.getPrimaryFlowPoolListInfo()
    },
    methods: {
      fileChangeHandle() {
        this.fileList.file = ''
        this.fileList.file = this.$refs.file.files[0]
        this.form.fileName = this.$refs.fileName.$el.innerText = this.$refs.file.files[0].name
        if (this.form.fileName.split('.').pop().toLowerCase() != 'xls'
          && this.form.fileName.split('.').pop().toLowerCase() != 'xlsx') {
          this.$message({ message: '请上传正确的EXCEL', type: 'error' })
        }
      },
      toBack() {
        this.$router.back(-1)
      },
      async getPrimaryFlowPoolListInfo() {
        const returnData = await editPrimaryFlowPoolList({
          id: this.firstPoolFlowId
        })
        //console.log(returnData)
        if (returnData._result) {
          this.form.flowPoolName = returnData.data.flowPoolName // 流量池名称
          if (returnData.data.dictOperator === 'TELECOM') {
            this.form.dictOperatorName = '电信'
          }
          if (returnData.data.dictOperator === 'MOBILE') {
            this.form.dictOperatorName = '移动'
          }
          if (returnData.data.dictOperator === 'UNICOM') {
            this.form.dictOperatorName = '联通'
          }
          this.form.singleCardMeal = returnData.data.singleCardMeal // 共享套餐
          this.form.theoryFlow = returnData.data.theoryFlow // 理论流量
          this.form.containsFlow = returnData.data.containsFlow // 实际流量
          this.form.virtualUsedFlow = returnData.data.virtualUsedFlow // 虚出流量
          this.form.channelCompanyId = returnData.data.channelCompanyId // 所属通道
          this.form.cardNumber = returnData.data.cardNumber // 挂卡数量
        }
      },
      async initData() {
        const returnData = await getChannelcompanyForSelect({
          id: this.firstPoolFlowId
        })
        if (returnData._result) {
          returnData.data.map(item => {
            this.form.dictOperatorNameSelect.push(item.dictOperatorName)
            this.form.channelCompanyIdSelect.push({ id: item.id, name: item.operatorBranch })
          })
        }
      },
      submitPrimaryFlowPoolHandle(form) {
        this.form.mealSpec = ''
        if (this.form.unit === 'G') {
          this.form.singleCardMeal = this.form.singleCardMeal * 1024
        }
        this.$refs[form].validate(async valid => {
          if (!this.fileList.file) {
            this.$message({ message: '请上传EXCEL', type: 'error' })
          }
          if (valid && this.fileList.file) {
            if (this.form.dictOperatorName === '电信') {
              this.dictOperator = 'TELECOM'
            }
            if (this.form.dictOperatorName === '移动') {
              this.dictOperator = 'MOBILE'
            }
            if (this.form.dictOperatorName === '联通') {
              this.dictOperator = 'UNICOM'
            }
            const returnData = await submitPrimaryFlowPool({
              id: this.firstPoolFlowId,
              file: this.fileList.file,
              channelCompanyId:this.form.channelCompanyId,
              singleCardMeal:this.form.singleCardMeal
            })
            if (returnData._result) {
              this.$router.back(-1)
              this.$message({ message: '流量池添加成功', type: 'success' })
            }
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .box-card {
    width: 666px;
    margin: 70px auto;
  }
  .file_upload_box {
    display: inline-block;
    width: 100%;
    height: auto;
    position: relative;
    overflow: hidden;
  }
  .file_upload_box input[type=file] {
    position: absolute;
    left: 0;
    top: 6px;
    width: 100%;
    line-height: 25px;
    opacity: 0;
    cursor: pointer;
  }
  .file_upload_box a {
    display: inline-block;
    width: 100%;
    line-height: 25px;
    text-align: center;
    font-family: "Microsoft yahei";
    background-color: #315baf;
    color: #FFF;
    font-weight: 700;
    text-decoration: none;
  }
  .file_upload_box /deep/ a {
    cursor: default;
  }
  .excel {
    cursor: default;
    display: inline-block;
    width: 50%;
    height: 25px;
    line-height: 0px;
    text-align: center;
    font-family: "Microsoft yahei";
    background-color: #315baf;
    color: #FFF;
    font-weight: 700;
    text-decoration: none;
  }
  .active {
    background: white;
    color: black;
    width: 100%;
    border: none;
    padding: 0;
  }
  .share {
    margin-bottom: -22px;
  }
</style>
<!--el-col(:xl="{ span: 20, push: 2 }")-->
<!--el-form-item(label="理论流量" label-width="110px" prop="theoryFlow")-->
<!--el-input(size="small" style="width: 100%;" v-model="form.theoryFlow")-->
<!--el-col(:xl="{ span: 20, push: 2 }")-->
<!--el-form-item(label="实际流量" label-width="110px" prop="containsFlow")-->
<!--el-input(size="small" style="width: 100%;" v-model="form.containsFlow")-->
<!--el-col(:xl="{ span: 20, push: 2 }")-->
<!--el-form-item(label="虚出流量" label-width="110px" prop="virtualUsedFlow")-->
<!--el-input(size="small" style="width: 100%;" v-model="form.virtualUsedFlow")-->
<!--<el-col :xl="{ span: 11 }" :lg="{ span: 12 }">
            <el-form-item prop="singleCardMeal">
              <el-input size="small" style="width: 300px;" v-model="form.singleCardMeal"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="unit" :xl="{ span: 11, push: 2 }" :lg="{ span: 12, push: 2 }">
            <el-form-item prop="unit">
              <el-select size="small" v-model="form.unit" placeholder="单位" style="width: 109%;">
                <el-option v-for="un in unitSelect" :key="un.value" :label="un.label" :value="un.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
